﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using School.Entities;
using System.Collections;
using System.IO;

namespace School.Admission
{
    public class StuffPagesCls
    {

        private static SchoolEntities db = new SchoolEntities();

        public static Int64 Add(ORG_Stuff_Pages StuffPage)
        {
            try
            {
                db.AddToORG_Stuff_Pages(StuffPage);

                db.SaveChanges();
                return StuffPage.Id;
            }
            catch
            {
                return -1;
            }
        }
        public static ORG_Stuff_Pages  Single(long id)
        {
            try
            {
                return db.ORG_Stuff_Pages.Single(x => x.Id == id);
            }
            catch
            {
                return null;
            }
        }
        public static long  Search(long Stuff_Id, long Page_Id)
        {
            try
            {
                ORG_Stuff_Pages obj =db.ORG_Stuff_Pages.Single(x => x.Stuff_Id == Stuff_Id && x.Page_Id== Page_Id );
                if (obj != null)
                {
                    return obj.Id;
                }
                else
                {
                    return -1;
                }
            }
            catch
            {
                return -1;
            }
        }
        public static long  GetByStuffId(long id, String PageName)
        {
            try
            {
                var query = from stuffPage in db.ORG_Stuff_Pages 
                            from c in db.ORG_Pages 
                            where stuffPage.Page_Id==c.Id 
                            where stuffPage.Stuff_Id == id
                            where c.Page_Name == PageName 

                            select stuffPage;
                IEnumerable<ORG_Stuff_Pages> z =  query.ToList();
                if (z.Count() == 0)
                {
                    return -1;
                }
                else
                {
                    return z.Count();
                }
            }
            catch
            {
                return -1;
            }
        }


        public static IEnumerable<ORG_Stuff_Pages> All(Hashtable items)
        {
            SchoolEntities db = new SchoolEntities();

            var query = from stuffPage in db.ORG_Stuff_Pages select stuffPage;
            foreach (DictionaryEntry item in items)
            {
                if (item.Key == "Id")
                {
                    long id;
                    id = Convert.ToInt64(item.Value);
                    query = query.Where(c => c.Id == id);
                }
            }
            return query.ToList();
        }

        public static IEnumerable<ORG_Pages> AllPages()
        {
            SchoolEntities db = new SchoolEntities();

            var query = from Page in db.ORG_Pages select Page;
            
            return query.ToList();
        }

        public static void Update(ORG_Stuff_Pages StuffPages)
        {

            ORG_Stuff_Pages c = db.ORG_Stuff_Pages.Single(x => x.Id == StuffPages.Id);
            c.Id = StuffPages.Id;
            c.Stuff_Id = StuffPages.Stuff_Id;
            c.Page_Id  = StuffPages.Page_Id;
            db.SaveChanges();

        }

        public static Boolean Delete(long id)
        {
            try
            {
                db.ORG_Stuff_Pages.DeleteObject(db.ORG_Stuff_Pages.Single(x => x.Id == id));
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static Boolean DeleteByStuffID(long StuffID)
        {

            try
            {
                var query = from c in db.ORG_Stuff_Pages 
                            where c.Stuff_Id== StuffID
                            select c;
                IEnumerable<ORG_Stuff_Pages> userPages = query.ToList();

                int i = 0;
                long pageId=0;

                while (i < userPages.Count())
                {
                    pageId = userPages.ElementAt(i).Id;
                    db.ORG_Stuff_Pages.DeleteObject(db.ORG_Stuff_Pages.Single(x => x.Id == pageId));
                db.SaveChanges();
                i += 1;
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
